#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{

        //open 函数打开的时候设置非阻塞
        //int fd = open("/dev/tty", O_RDWR| O_NONBLOCK);
        
        //fcntl()函数,设置非阻塞
        int fd = open("/dev/tty", O_RDWR);
        int flags = fcntl(fd, F_GETFL);
        flags |= O_NONBLOCK;
        fcntl(fd, F_SETFL, flags);

        char buf[1024] = {0};
        int ret = 0;
        while (1){
                ret = read(fd, buf, sizeof(buf));
                if(ret < 0){
                        perror("read err: ");
                        printf("ret is %d\n", ret);
                }
                if(ret){
                        printf("buf is %s\n", buf);
                }
                printf("haha\n");
                sleep(1);
        }
        close(fd);
        return 0;
}
